<template>
  <div class="top-list" v-loading="loading">
    <Scroll class="top-list-content">
      <ul>
        <li class="item" v-for="item in topList" :key="item.id" @click="selectItem(item)">
          <div class="icon">
            <img
              style="
                 {
                  width: 4.26rem;
                  height: 4.26rem;
                }
              "
              v-lazy="item.pic"
            />
          </div>
          <ul class="song-list">
            <li class="song" v-for="(song, index) in item.songList" :key="song.id">
              <span>{{ index + 1 }}.</span>
              <span>{{ song.songName }}-{{ song.singerName }}</span>
            </li>
          </ul>
        </li>
      </ul>
    </Scroll>
    <router-view v-slot="{ Component }">
      <transition name="slide">
        <component :is="Component" :data='selectedTop'/>
      </transition>
    </router-view>
  </div>
</template>
<script>
import Scroll from '@/components/base/scroll/scroll.vue'
import { getTopList } from '@/service/top-list'
import storage from 'good-storage'
import { TOP_KEY } from '@/js/constant'
export default {
  name: 'TopList',
  components: {
    Scroll
  },
  data () {
    return {
      topList: [],
      loading: true,
      selectedTop: null
    }
  },
  async created () {
    const result = await getTopList()
    this.topList = result.topList
    this.loading = false
  },
  methods: {
    selectItem (top) {
      this.cachedTop(top)
      this.selectedTop = top
      this.$router.push({
        path: `/top-list/${top.id}`
      })
    },
    cachedTop (top) {
      storage.set(TOP_KEY, top)
    }
  }
}
</script>
<style lang="scss" scoped>
.top-list {
  position: fixed;
  width: 100%;
  top: 3.752665rem;
  bottom: 0;
  .top-list-content {
    height: 100%;
    overflow: hidden;
    .item {
      display: flex;
      margin: 0 .852878rem;
      padding-top: .852878rem;
      height: 4.264392rem;
      &:last-child {
        padding-bottom: .852878rem;
      }
      .icon {
        flex: 0 0 4.264392rem;
        width: 4.264392rem;
        height: 4.264392rem;
      }
      .song-list {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 0 .852878rem;
        height: 4.264392rem;
        overflow: hidden;
        background: $color-highlight-background;
        color: $color-text-d;
        font-size: $font-size-small;
        .song {
          @include no-wrap();
          line-height: 1.108742rem;
        }
      }
    }
  }
}
</style>
